//
//  GFMagazineTableViewCell.m
//  GoodFood
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFMagazineTableViewCell.h"

@interface GFMagazineTableViewCell ()
@property (nonatomic, strong) UIImageView *mainImageView;
@property (nonatomic, strong) UIImageView *routeFirstImageView;//旋转装饰第一张
@property (nonatomic, strong) UIImageView *routeSecondImageView;//旋转装饰第二张
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) UIView *smallView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIImageView *userImageView;
@property (nonatomic, strong) UILabel *userLabel;
@end

@implementation GFMagazineTableViewCell
#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor lightGrayColor];
        [self createView];
        [self createLabel];
        [self creatImageView];
        [self.contentView bringSubviewToFront:self.smallView];
        
        
    }
    return self;
}

#pragma mark - 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    self.mainView.frame = CGRectMake(5, 5, ScreenWidth - 10, HEIGHT - 10);
    self.smallView.frame = CGRectMake(ScreenWidth / 15 + 5, ScreenWidth / 15 + 5, ScreenWidth / 3, ScreenWidth / 3);
    self.mainImageView.frame = CGRectMake(5, 5, self.smallView.frame.size.width - 10, self.smallView.frame.size.width - 10);
    CGRect rectForTitle = [[NSString stringWithFormat:@"刊号:%@", self.model.serial_number] boundingRectWithSize:CGSizeMake(ScreenWidth - 35 - ScreenWidth / 3 - ScreenWidth / 15, HEIGHT / 15 * 2.5) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    self.titleLabel.frame = CGRectMake(ScreenWidth / 15 + 25 + ScreenWidth / 3 , 5 + ScreenWidth / 15, rectForTitle.size.width, rectForTitle.size.height);
//    self.titleLabel.backgroundColor = [UIColor yellowColor];
    CGRect rectForContent = [self.model.title boundingRectWithSize:CGSizeMake(self.mainView.frame.size.width - 25 - ScreenWidth / 15 - ScreenWidth / 3, HEIGHT - HEIGHT / 15 * 2.5 - 5 - HEIGHT / 15 * 2.5 - ScreenWidth / 15) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil];
    self.contentLabel.frame = CGRectMake(ScreenWidth / 15 + ScreenWidth / 3 + 25, 5 + HEIGHT / 15 * 2.5 + ScreenWidth / 15, rectForContent.size.width, rectForContent.size.height);
//    self.contentLabel.backgroundColor = [UIColor greenColor];
    
    self.userImageView.frame = CGRectMake(ScreenWidth / 15 + ScreenWidth / 3 + 25, 10 + HEIGHT / 15 * 2.5 + ScreenWidth / 15 + rectForContent.size.height, ScreenWidth / 15, ScreenWidth / 15);
    self.userLabel.frame = CGRectMake(self.userImageView.frame.origin.x + ScreenWidth / 15 + 5 , self.userImageView.frame.origin.y, self.mainView.frame.size.width - 5 -ScreenWidth/15-ScreenWidth/3-25-ScreenWidth/15, ScreenWidth / 15);
//    self.userLabel.backgroundColor = [UIColor orangeColor];
    

}
#pragma mark - set
- (void)setModel:(GFMagazineModel *)model {
    if (_model != model) {
        _model = model;
    }
    //杂志图片
    [self.mainImageView setImageWithURL:[NSURL URLWithString:model.picture_url] placeholderImage:[UIImage imageNamed:@"category_1"]];
    //标题
    self.titleLabel.text = [NSString stringWithFormat:@"刊号:%@", model.serial_number];
    //内容
    self.contentLabel.text = model.title;
    //用户头像
    [self.userImageView setImageWithURL:[NSURL URLWithString:model.user_image] placeholderImage:[UIImage imageNamed:@"category_1"]];
    //用户名称
    self.userLabel.text = [NSString stringWithFormat:@"主编:%@", model.user_name];
}
#pragma mark - 创建imageView
- (void)creatImageView {

//    [self.contentView addSubview:self.mainImageView];
    
    self.routeFirstImageView = [[UIImageView alloc] init];

    [self.mainView addSubview:self.routeFirstImageView];
    self.routeFirstImageView.layer.masksToBounds = YES;
    self.routeFirstImageView.layer.cornerRadius = 5;
    self.routeFirstImageView.layer.borderWidth = 0.3;
    self.routeFirstImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.routeFirstImageView.layer setShadowColor:[UIColor whiteColor].CGColor];
    self.routeFirstImageView.backgroundColor = [UIColor whiteColor];
    [self.routeFirstImageView.layer setShadowOpacity:0.8];
    [self.routeFirstImageView.layer setOpacity:1];
    [self.routeFirstImageView.layer setShadowOffset:CGSizeMake(4, 3)];
    self.routeFirstImageView.frame = CGRectMake(ScreenWidth / 15 + 5, ScreenWidth / 15 + 5, ScreenWidth / 3, ScreenWidth / 3);
    self.routeFirstImageView.transform = CGAffineTransformMakeRotation(20 * M_PI / 180.f);
    
    self.routeSecondImageView = [[UIImageView alloc] init];
    [self.mainView addSubview:self.routeSecondImageView];
    self.routeSecondImageView.layer.masksToBounds = YES;
    self.routeSecondImageView.layer.cornerRadius = 5;
    self.routeSecondImageView.layer.borderWidth = 0.3;
    self.routeSecondImageView.backgroundColor = [UIColor whiteColor];
    self.routeSecondImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.routeSecondImageView.layer setShadowColor:[UIColor whiteColor].CGColor];
    [self.routeSecondImageView.layer setShadowOpacity:0.8];
    [self.routeSecondImageView.layer setOpacity:1];
    [self.routeSecondImageView.layer setShadowOffset:CGSizeMake(4, 3)];
    self.routeSecondImageView.frame = CGRectMake(ScreenWidth / 15 + 5, ScreenWidth / 15 + 5, ScreenWidth / 3, ScreenWidth / 3);
    self.routeSecondImageView.transform = CGAffineTransformMakeRotation(10 * M_PI / 180.f);
    
    self.smallView = [[UIView alloc] init];
    [self.mainView addSubview:self.smallView];
    self.smallView.layer.masksToBounds = YES;
    self.smallView.layer.cornerRadius = 5;
    self.smallView.layer.borderWidth = 0.3;
    self.smallView.backgroundColor = [UIColor whiteColor];
    self.smallView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    self.mainImageView = [[UIImageView alloc] init];
    [self.smallView addSubview:self.mainImageView];
//    [self.contentView bringSubviewToFront:self.mainImageView];
//    self.mainImageView.layer.masksToBounds = YES;
//    self.mainImageView.layer.cornerRadius = 5;
    
    self.userImageView = [[UIImageView alloc] init];
    [self.mainView addSubview:self.userImageView];

    
}
#pragma mark - 创建view
- (void)createView {
    self.mainView = [[UIView alloc] init];
    [self.contentView addSubview:self.mainView];
    self.mainView.layer.masksToBounds = YES;
    self.mainView.layer.borderWidth = 0.3;
    self.mainView.layer.cornerRadius = 5;
    self.mainView.backgroundColor = [UIColor whiteColor];
    

}
#pragma mark - 创建label
- (void)createLabel {
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    self.titleLabel.textColor = [UIColor redColor];
    self.titleLabel.numberOfLines = 0;
    [self.mainView addSubview:self.titleLabel];
    
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.font = [UIFont systemFontOfSize:13];
    self.contentLabel.numberOfLines = 0;
    [self.mainView addSubview:self.contentLabel];
    
    self.userLabel = [[UILabel alloc] init];
    self.userLabel.textColor = [UIColor lightGrayColor];
    self.userLabel.font = [UIFont systemFontOfSize:12];
    self.userLabel.numberOfLines = 0;
    [self.mainView addSubview:self.userLabel];
}
@end
